<template>
  <div>
    <el-button type="primary" :disabled="baned" @click="buygoods" :loading="buttonLoad">购买</el-button>
    <el-button type="primary" @click="paygoods">支付</el-button>

  </div>
</template>

<script>
import request from "../../../utils/request";

export default {
  name: "Label",
  data() {
    return {
      sid:2,
      userid:1,
      hash:"",
      buttonLoad:false,
      baned: false,
      timer:"", // 定时器,循环确认是否抢购成功
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标签信息表格数据
      labelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        revision: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //this.getList();
  },
  methods: {
    buygoods() {
      this.buttonLoad = true
      request.get("/drug-management/getHash?sid="+this.sid+"&userId="+this.userid).then(res => {
        console.log(res)
        if (res === "获取验证hash失败") {
          this.buttonLoad = false
          console.log("验证失败");
        } else if (res == "布隆过滤器中不存在该商品") {
          this.buttonLoad = false
          console.log("此商品现不能秒杀")
        } else { // 获取抢购链接md5成功
          //console.log(res.split("：")[1]);
          this.hash = res.split("：")[1];// 此后抢购操作需要携带该值进行访问
          request.get("/drug-management/createOrderWithMD5UrlAndLimit?sid="+this.sid+"&userId="+this.userid+"&verifyHash="+this.hash).then(res => {
            //this.buttonLoad = false
            console.log(res)
            if (res == "秒杀请求提交成功") {
              this.timer = setInterval(this.verifyBuy, 1000);
            } else {
              this.buttonLoad = false
              console.log("秒杀请求提交失败"+res)
            }
          })
        }
      })
    },
    verifyBuy() {
      request.get("/drug-management/checkOrderByUserIdInCache?sid="+this.sid+"&userId="+this.userid).then(res =>{
        console.log(res)
        if (res == "恭喜您，已经抢购成功！") {
          this.buttonLoad = false
          clearInterval(this.timer)
        }
      })
    },
    paygoods() {
      //clearInterval(this.timer)
      request.get("/drug-management/")
    },
    /** 查询标签信息列表 */
    getList() {
      this.loading = true;
      listLabel(this.queryParams).then(response => {
        this.labelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        labelId: null,
        name: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.labelId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加标签信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const labelId = row.labelId || this.ids
      getLabel(labelId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改标签信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.labelId != null) {
            updateLabel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLabel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const labelIds = row.labelId || this.ids;
      this.$modal.confirm('是否确认删除标签信息编号为"' + labelIds + '"的数据项？').then(function() {
        return delLabel(labelIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hospital/label/export', {
        ...this.queryParams
      }, `label_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
